package com.seari.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * 日期工具类
 */
public final class DateUtil {
	
	/**
	 * 时间格式：yyyy-MM-dd HH:mm:ss
	 */
    public static final String TIME_FORMAT_SIMPLE1 = new String("yyyy-MM-dd HH:mm:ss");
	/**
	 * 时间格式：yyyy/MM/dd HH:mm:ss
	 */
    public static final String TIME_FORMAT_SIMPLE2 = new String("yyyy/MM/dd HH:mm:ss");
	/**
	 * 时间格式：yyyyMMddHHmmss
	 */
    public static final String TIME_FORMAT_SIMPLE3 = new String("yyyyMMddHHmmss");
	/**
	 * 时间格式：yyyy年MM月dd日 HH点mm分ss秒
	 */
    public static final String TIME_FORMAT_SIMPLE4 = new String("yyyy年MM月dd日 HH点mm分ss秒");
    /**
     * 时间格式：yyyy-MM-dd HH:mm
     */
    public static final String TIME_FORMAT_SIMPLE5 = new String("yyyy-MM-dd HH:mm");
    /**
     * 时间格式：yyyyMMdd HH:mm:ss
     */
    public static final String TIME_FORMAT_SIMPLE6 = new String("yyyyMMdd HH:mm:ss");

	/**
	 * 时间格式：yyyyMMdd HH:mm:ss
	 */
	public static final String TIME_FORMAT_SIMPLE7 = new String("yyyy-MM-dd HH-mm-ss");
    
    /**
     * 时间格式：HH:mm:ss
     */
    public static final String TIME_FORMAT_HHMMSS = new String("HH:mm:ss");
    /**
     * 时间格式：HH:mm
     */
    public static final String TIME_FORMAT_HHMM = new String("HH:mm");
	/**
	 * 日期格式：yyyyMMdd
	 */
    public static final String DATE_FORMAT_YYYYMMDD1 = new String("yyyyMMdd000000");
	/**
	 * 日期格式：yyyy-MM-dd
	 */
    public static final String DATE_FORMAT_YYYYMMDD2 = new String("yyyy-MM-dd");
    /**
     * 日期格式：yyyymm
     */
    public static final String DATE_FORMAT_YYYYMM = new String("yyyyMM");
    /**
     * 日期格式：yyyy-mm
     */
    public static final String DATE_FORMAT_YYYY_MM = new String("yyyy-MM");
    
    public static String yMd6 = new String("yyyyMMdd060000");//6点赠费
    public static String yMd18 = new String("yyyyMMdd180000");//18点赠费

	/**
	 * 字符串类型转时间类型
	 * @param dateStr
	 * @param formatStr
	 * @return
	 */
    public static final Date StringToDate(String dateStr,String formatStr){
		SimpleDateFormat sf = new SimpleDateFormat(formatStr);
		Date date = null;
		try {
			date = sf.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 时间类型转字符串类型
	 * @param date
	 * @param formatStr
	 * @return
	 */
    public static final String DateToString(Date date,String formatStr){
		SimpleDateFormat sf = new SimpleDateFormat(formatStr);
		System.out.println(sf.format(date));
		return sf.format(date);
	}

	/**
	 * 日期格式转换
	 * @param dateStr
	 * @param fromFormat
	 * @param toFormat
	 * @return
	 */
    public static final String stringToString(String dateStr,String fromFormat,String toFormat){
		Date d = StringToDate(dateStr,fromFormat);
		String str = DateToString(d,toFormat);
		return str;
	}

	/**
	 * 将数据库时间格式转换为字符串(兼容oracle、mysql)
	 * @param timestap
	 * @param formatStr
	 * @return
	 */
//    public static final String timestamp2String(Timestamp timestap,String formatStr){
//    	if(HandlerNullUtils.objectIsNull(timestap)){
//    		return  null;
//    	}
//    	SimpleDateFormat sf = new SimpleDateFormat(formatStr);
//        return sf.format(timestap);
//    }

	/**
	 * 获取当前时间前几分钟
	 * @param date
	 * @return
	 */
	public static final Date beforeSeveralMinuteTime(Date date,Integer i){
		Calendar beforeTime = Calendar.getInstance();
		beforeTime.add(Calendar.MINUTE, i);
		Date beforeD = beforeTime.getTime();
		return beforeD;
	}
	
	/**
	 * 获取指定时间后五分钟
	 * @param date
	 * @return
	 */
	public static final Date after5MinuteTimeFromDate(Date date){
		long sourceTime = date.getTime();
		return new Date(sourceTime + 5 * 1000 * 60);
	}

	/**
	 * 获取当前时间前X分钟的Y分钟倍数时间
	 * @param date
	 * @return
	 */
	public static Date beforeXMinuteTimeWithInterval(Date date,int x,int y)
	{
		Calendar beforeTime = Calendar.getInstance();
		beforeTime.add(Calendar.MINUTE, -x);
		int minute = beforeTime.get(Calendar.MINUTE);
		beforeTime.set(Calendar.MINUTE,((int)(minute / y))*y); //去除零散分钟，仅取Y分钟的倍数
		beforeTime.set(Calendar.SECOND,0);
		beforeTime.set(Calendar.MILLISECOND,0); //设置毫秒数为0，防止MYSQL对时间戳四舍五入产生1秒误差
		Date beforeD = beforeTime.getTime();
		return beforeD;
	}

    /**
	 * 获取指定时间24小时后时间
	 * @param date
	 * @return
	 */
	public static final Date after24HoursTimeFromDate(Date date){
		long sourceTime = date.getTime();
		return new Date(sourceTime + 1440*1000*60);
	}
	
	/**
	 * 获取指定时间80天后时间
	 * @param date
	 * @return
	 */
	public static final Date after80DayFromDate(Date date){
		long sourceTime = date.getTime();
		return new Date(sourceTime + 1440*1000*60*80);
	}

	/**
	 * 获取指定时间24小时前时间
	 * @param date
	 * @return
	 */
	public static final Date before24HoursTimeFromDate(Date date){
		long sourceTime = date.getTime();
		return new Date(sourceTime - 1440*1000*60);
	}

	/**
	 * 获取指定时间2年前时间
	 * @param date
	 * @return
	 */
	public static final Date before2YearsTimeFromDate(Date date){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.YEAR, -2);
		Date y = c.getTime();
		return y;
	}
	
    /**
     * 把日期转换为字符串
     */
    public static String dateToString(java.util.Date date, String format) {
        String result = "";
        SimpleDateFormat formater = new SimpleDateFormat(format);
        try {
            result = formater.format(date);
        } catch (Exception e) {
            // log.error(e);
        }
        return result;
    }
	
	/**
     * 获取当前时间的指定格式
     */
    public static String getCurrDate(String format) {
        return dateToString(new Date(), format);
    }
	


	public static void main(String[] args) {
	//	System.out.println(DateUtil.DateToString(new Date(),DateUtil.TIME_FORMAT_SIMPLE7));
        Date date = new Date();
        Date date1 = beforeXMinuteTimeWithInterval(date,1440,10);
        System.out.println("***********");
		System.out.println(DateUtil.DateToString(date1,DateUtil.TIME_FORMAT_SIMPLE7));
        System.out.println(DateUtil.DateToString(after24HoursTimeFromDate(date1),DateUtil.TIME_FORMAT_SIMPLE7));

	}
}
